home *** CD-ROM | disk | FTP | other *** search
/ Champak 106 / Vol 106.iso / games / insuranc.swf / scripts / __Packages / Car.as < prev    next >
Encoding:
Text File  |  2010-04-12  |  12.2 KB  |  422 lines

  1. class Car
  2. {
  3.    var pos;
  4.    var vel;
  5.    var acc;
  6.    var maxVel;
  7.    var slideTime;
  8.    var sEnemy;
  9.    var energy;
  10.    var blinkInd;
  11.    var throwingTime;
  12.    var autoTurnFactor;
  13.    var bDestroyed;
  14.    var bFromWeapon;
  15.    var mcCanvas;
  16.    var mcCar;
  17.    var width;
  18.    var height;
  19.    var waitTime;
  20.    var frame;
  21.    var mcDamage;
  22.    var lastFrame;
  23.    var firstFrame;
  24.    var turnDelay;
  25.    var turnTime;
  26.    var turnDir;
  27.    function Car(sLinkageId, newPos, newVel, mcCanvasNew, depth, waitTimeNew)
  28.    {
  29.       this.pos = newPos.clone();
  30.       this.vel = newVel.clone();
  31.       this.acc = new flash.geom.Point(0,0);
  32.       this.maxVel = 20;
  33.       this.slideTime = 0;
  34.       this.sEnemy = Game.getInstance().sEnemy;
  35.       this.energy = 4;
  36.       this.blinkInd = 0;
  37.       this.throwingTime = 5;
  38.       this.autoTurnFactor = 20;
  39.       this.bDestroyed = false;
  40.       this.bFromWeapon = false;
  41.       this.mcCanvas = mcCanvasNew;
  42.       this.mcCar = this.mcCanvas.attachMovie(sLinkageId,"mcCar" + depth,depth);
  43.       this.width = this.mcCar._width;
  44.       this.height = this.mcCar._height;
  45.       this.waitTime = waitTimeNew;
  46.       this.frame = 1;
  47.       this.setFramesRange();
  48.       this.setTurnDelay();
  49.       this.draw();
  50.    }
  51.    function remove(Void)
  52.    {
  53.       this.mcCar.removeMovieClip();
  54.       this.mcDamage.removeMovieClip();
  55.       false;
  56.    }
  57.    function step(plPos, plVel, bmpShoulder)
  58.    {
  59.       if(this.waitTime > 0)
  60.       {
  61.          this.waitTime = this.waitTime - 1;
  62.          return undefined;
  63.       }
  64.       this.frame = this.frame + 1;
  65.       if(this.frame > this.lastFrame)
  66.       {
  67.          this.frame = this.firstFrame;
  68.       }
  69.       this.vel.x += this.acc.x;
  70.       this.vel.y += this.acc.y;
  71.       this.acc.x = 0;
  72.       this.acc.y = 0;
  73.       if(this.bDestroyed)
  74.       {
  75.          this.modifyVelocity(plVel);
  76.          this.throwingTime = this.throwingTime - 1;
  77.       }
  78.       else
  79.       {
  80.          if(this.slideTime > 0)
  81.          {
  82.             this.slideTime = this.slideTime - 1;
  83.          }
  84.          else
  85.          {
  86.             this.vel.x *= 0.7;
  87.          }
  88.          if(Math.abs(this.vel.x) < 0.1)
  89.          {
  90.             this.vel.x = 0;
  91.          }
  92.       }
  93.       this.pos.x += this.vel.x * this.vel.y / this.maxVel;
  94.       this.pos.y += this.vel.y - plVel.y;
  95.       if(!this.bDestroyed)
  96.       {
  97.          this.turnDelay = this.turnDelay - 1;
  98.          if(this.turnDelay == 0)
  99.          {
  100.             this.setTurnDelay();
  101.          }
  102.          else if(this.turnDelay < this.turnTime)
  103.          {
  104.             this.acc.x = this.turnDir * this.vel.y / this.autoTurnFactor;
  105.          }
  106.          var _loc2_ = this.pos.y + 150;
  107.          if(_loc2_ > Game.screenH)
  108.          {
  109.             _loc2_ = Game.screenH;
  110.          }
  111.          _loc2_ = Game.screenH - _loc2_;
  112.          var _loc5_ = new flash.geom.Point(this.pos.x - this.width / 2 - 10,_loc2_);
  113.          var _loc4_ = new flash.geom.Point(this.pos.x + this.width / 2 + 10,_loc2_);
  114.          if(this.slideTime == 0)
  115.          {
  116.             if(bmpShoulder.hitTest(new flash.geom.Point(0,0),250,_loc5_))
  117.             {
  118.                this.acc.x = this.vel.y / 4;
  119.                this.setTurnDelay();
  120.             }
  121.             else if(bmpShoulder.hitTest(new flash.geom.Point(0,0),250,_loc4_))
  122.             {
  123.                this.acc.x = (- this.vel.y) / 4;
  124.                this.setTurnDelay();
  125.             }
  126.          }
  127.          if(bmpShoulder.hitTest(new flash.geom.Point(0,0),250,new flash.geom.Point(this.pos.x,Game.screenH - this.pos.y)))
  128.          {
  129.             if(this.slideTime > 0)
  130.             {
  131.                this.blinkInd = 0;
  132.                this.getDamage(4);
  133.             }
  134.             else
  135.             {
  136.                this.reactionOnShoulder(bmpShoulder);
  137.             }
  138.          }
  139.       }
  140.       if(this.pos.x < (- this.width) / 2 || this.pos.x > Game.screenW + this.width / 2)
  141.       {
  142.          this.onOutOfScreenH();
  143.       }
  144.       if(this.pos.y < -60 || this.pos.y > Game.screenH + 60)
  145.       {
  146.          this.onOutOfScreenV();
  147.       }
  148.       if(this.blinkInd > 0)
  149.       {
  150.          this.blinkInd = this.blinkInd - 1;
  151.          this.blink();
  152.       }
  153.    }
  154.    function draw(Void)
  155.    {
  156.       this.mcCar.gotoAndStop(this.frame);
  157.       this.mcCar._x = this.pos.x;
  158.       this.mcCar._y = Game.screenH - this.pos.y;
  159.       if(this.bDestroyed)
  160.       {
  161.          this.mcCar._rotation += this.vel.y >= 2 ? this.vel.y * 3 : 6;
  162.          this.mcDamage._x = this.mcCar._x;
  163.          this.mcDamage._y = this.mcCar._y;
  164.       }
  165.       else
  166.       {
  167.          this.mcCar._rotation = this.vel.x * Math.ceil(this.vel.y / this.maxVel);
  168.       }
  169.    }
  170.    function getRect(Void)
  171.    {
  172.       var _loc2_ = this.mcCar.getRect(this.mcCanvas);
  173.       return new flash.geom.Rectangle(_loc2_.xMin,_loc2_.yMin,_loc2_.xMax - _loc2_.xMin,_loc2_.yMax - _loc2_.yMin);
  174.    }
  175.    function reactionOnShoulder(bmpShoulder)
  176.    {
  177.       if(bmpShoulder.hitTest(new flash.geom.Point(0,0),250,new flash.geom.Point(this.pos.x + 20,Game.screenH - this.pos.y)))
  178.       {
  179.          this.vel.x = (- this.vel.y) / 2;
  180.          this.pos.x += this.vel.x;
  181.       }
  182.       else
  183.       {
  184.          this.vel.x = this.vel.y / 2;
  185.          this.pos.x += this.vel.x;
  186.       }
  187.    }
  188.    function getDamage(value)
  189.    {
  190.       if(this.blinkInd == 0)
  191.       {
  192.          this.blinkInd = 10;
  193.          if(value == undefined)
  194.          {
  195.             this.energy -= 1;
  196.          }
  197.          else
  198.          {
  199.             this.energy -= value;
  200.          }
  201.       }
  202.       if(this.energy <= 0 && !this.bDestroyed)
  203.       {
  204.          this.vel.y /= 2;
  205.          this.bDestroyed = true;
  206.          if(this.bFromWeapon)
  207.          {
  208.             var _loc2_ = Game.getInstance().sEnemy;
  209.             Game.getInstance().addPoints(-200);
  210.             Game.getInstance().showPoints(this.pos,-200);
  211.             switch(_loc2_)
  212.             {
  213.                case "tesco":
  214.                   var _loc3_ = this.mcCar.getDepth() + 1;
  215.                   this.mcDamage = this.mcCanvas.attachMovie("car damage","mcDamage" + _loc3_,_loc3_);
  216.                   this.mcDamage.gotoAndStop(_loc2_);
  217.                   Sounds.playSound("skid");
  218.                   break;
  219.                case "directLine":
  220.                   _loc3_ = this.mcCar.getDepth() + 1;
  221.                   this.mcDamage = this.mcCanvas.attachMovie("car damage","mcDamage" + _loc3_,_loc3_);
  222.                   this.mcDamage.gotoAndStop(_loc2_);
  223.                   this.mcCar._visible = false;
  224.                   Sounds.playSound("vapourise");
  225.                   break;
  226.                case "sainsbury":
  227.                   _loc3_ = this.mcCar.getDepth() + 1;
  228.                   this.mcDamage = this.mcCanvas.attachMovie("car damage","mcDamage" + _loc3_,_loc3_);
  229.                   this.mcDamage.gotoAndStop(_loc2_);
  230.                   this.mcCar._visible = false;
  231.                   break;
  232.                default:
  233.                   Sounds.playSound("skid");
  234.             }
  235.          }
  236.          else
  237.          {
  238.             Game.getInstance().addPoints(-200);
  239.             Game.getInstance().showPoints(this.pos,-200);
  240.             Sounds.playSound("skid");
  241.          }
  242.       }
  243.       else
  244.       {
  245.          this.setFramesRange();
  246.          this.frame = this.firstFrame;
  247.       }
  248.    }
  249.    function die(Void)
  250.    {
  251.       Game.getInstance().addRandomCar();
  252.       Game.getInstance().removeRandomCar(this);
  253.    }
  254.    function setFramesRange(Void)
  255.    {
  256.       this.firstFrame = (4 - this.energy) * 25 + 1;
  257.       if(this.firstFrame > 76)
  258.       {
  259.          this.firstFrame = 76;
  260.       }
  261.       this.lastFrame = this.firstFrame + 24;
  262.    }
  263.    function setTurnDelay(Void)
  264.    {
  265.       this.turnDelay = 60 + Math.round(30 * Math.random());
  266.       this.turnTime = 10 + Math.round(20 * Math.random());
  267.       this.turnDir = Math.random() >= 0.5 ? 1 : -1;
  268.       this.autoTurnFactor = 20;
  269.    }
  270.    function checkDistance(refCar)
  271.    {
  272.       if(this.bDestroyed || this.waitTime > 0)
  273.       {
  274.          return undefined;
  275.       }
  276.       var _loc4_ = refCar.pos.x - this.pos.x;
  277.       var _loc2_ = refCar.pos.y - this.pos.y;
  278.       var _loc5_ = (this.width + refCar.width) / 2;
  279.       var _loc6_ = (this.height + refCar.height) / 2;
  280.       if(_loc2_ > 0 && _loc2_ < 150 && Math.abs(_loc4_) < _loc5_ && this.vel.y > refCar.vel.y)
  281.       {
  282.          this.acc.y = -1;
  283.       }
  284.       if(Math.abs(_loc2_) < _loc6_ && Math.abs(_loc4_) < _loc5_ + 10)
  285.       {
  286.          this.acc.x = 0;
  287.          this.vel.x = 0;
  288.          this.setTurnDelay();
  289.       }
  290.    }
  291.    function checkCollision(refCar, bTakeDamage)
  292.    {
  293.       if(this.bDestroyed || this.waitTime > 0 || refCar.bExit)
  294.       {
  295.          return undefined;
  296.       }
  297.       var _loc5_ = refCar.pos.x - this.pos.x;
  298.       var _loc7_ = refCar.pos.y - this.pos.y;
  299.       var _loc9_ = (this.width + refCar.width) / 2;
  300.       var _loc8_ = (this.height + refCar.height) / 2;
  301.       if(Math.abs(_loc7_) < _loc8_ && Math.abs(_loc5_) < _loc9_)
  302.       {
  303.          var _loc3_ = this.vel.subtract(refCar.vel);
  304.          var _loc11_ = !Application.bEasy ? 6 : 3;
  305.          var _loc4_ = 10 * Math.floor(_loc3_.length / refCar.maxVel * _loc11_);
  306.          if(_loc5_ > 0)
  307.          {
  308.             if(bTakeDamage)
  309.             {
  310.                this.vel.x = (- _loc3_.length) * 0.7;
  311.                this.slideTime = 15;
  312.             }
  313.             else
  314.             {
  315.                this.vel.x = (- this.vel.y) / 4;
  316.             }
  317.             refCar.vel.x = refCar.vel.y / 4;
  318.          }
  319.          else
  320.          {
  321.             if(bTakeDamage)
  322.             {
  323.                this.vel.x = _loc3_.length * 0.7;
  324.                this.slideTime = 15;
  325.             }
  326.             else
  327.             {
  328.                this.vel.x = this.vel.y / 4;
  329.             }
  330.             refCar.vel.x = (- refCar.vel.y) / 4;
  331.          }
  332.          var _loc10_ = this.vel.y;
  333.          this.vel.y = refCar.vel.y * 0.95;
  334.          refCar.vel.y = _loc10_ * 0.95;
  335.          this.pos.x += this.vel.x;
  336.          this.pos.y += this.vel.y;
  337.          refCar.pos.x += refCar.vel.x;
  338.          refCar.pos.y += refCar.vel.y;
  339.          if(bTakeDamage && !refCar.bShield)
  340.          {
  341.             refCar.getDamage(_loc4_);
  342.             if(Application.bEasy)
  343.             {
  344.                this.getDamage(1 + _loc4_ / 10);
  345.             }
  346.             else
  347.             {
  348.                this.getDamage(_loc4_ / 10);
  349.             }
  350.             Sounds.playSound("carCrash");
  351.          }
  352.       }
  353.    }
  354.    function checkWeapon(mcWeapon)
  355.    {
  356.       var _loc2_ = Game.getInstance().sEnemy;
  357.       if(_loc2_ == "churchil" || _loc2_ == "asda" || _loc2_ == "lloyd" || _loc2_ == "elephant")
  358.       {
  359.          return undefined;
  360.       }
  361.       if((_loc2_ == "directLine" || _loc2_ == "aa") && mcWeapon.hitTest(this.mcCar._x,this.mcCar._y) || _loc2_ != "directLine" && _loc2_ != "aa" && mcWeapon.hitTest(this.mcCar))
  362.       {
  363.          this.energy = 0;
  364.          this.bFromWeapon = true;
  365.          this.getDamage();
  366.          this.setFramesRange();
  367.          this.frame = this.firstFrame;
  368.          if(_loc2_ == "sainsbury")
  369.          {
  370.             Game.getInstance().removeShot();
  371.          }
  372.       }
  373.    }
  374.    function blink(Void)
  375.    {
  376.       var _loc4_ = undefined;
  377.       var _loc2_ = Math.round(255 * this.blinkInd / 10);
  378.       _loc4_ = new flash.geom.ColorTransform(1,1,1,1,_loc2_,_loc2_,_loc2_,0);
  379.       var _loc3_ = new flash.geom.Transform(this.mcCar);
  380.       _loc3_.colorTransform = _loc4_;
  381.    }
  382.    function modifyVelocity(plVel)
  383.    {
  384.       if(this.bFromWeapon)
  385.       {
  386.          var _loc3_ = Game.getInstance().sEnemy;
  387.          switch(_loc3_)
  388.          {
  389.             case "tesco":
  390.                this.vel.y = plVel.y - 5;
  391.                break;
  392.             case "directLine":
  393.                this.vel.y = plVel.y - 5;
  394.                break;
  395.             case "sainsbury":
  396.                this.vel.y = plVel.y - 5;
  397.                break;
  398.             case "moreThan":
  399.                this.vel.y = plVel.y + 15;
  400.                this.vel.x -= 1;
  401.                break;
  402.             default:
  403.                this.vel.y = plVel.y - 8;
  404.          }
  405.       }
  406.       else
  407.       {
  408.          this.vel.y = plVel.y - 2;
  409.          this.vel.x = Math.abs(this.vel.x) / this.vel.x * 8;
  410.          trace("vel: " + this.vel);
  411.       }
  412.    }
  413.    function onOutOfScreenV(Void)
  414.    {
  415.       this.die();
  416.    }
  417.    function onOutOfScreenH(Void)
  418.    {
  419.       this.die();
  420.    }
  421. }
  422.